package com.wondersgroup.framework.widget.library.extjs;

import com.wondersgroup.framework.widget.codegen.CodeGenerator;
import com.wondersgroup.framework.widget.codegen.annotation.CodeMethod;
import com.wondersgroup.framework.widget.codegen.annotation.ObjectField;
import com.wondersgroup.framework.widget.codegen.annotation.ObjectMapping;
import com.wondersgroup.framework.widget.codegen.expression.Expression;
import com.wondersgroup.framework.widget.codegen.expression.ExpressionFactory;
import com.wondersgroup.framework.widget.codegen.expression.JSFactory;
import com.wondersgroup.framework.widget.codegen.process.BaseCodeObject;
import com.wondersgroup.framework.widget.codegen.process.CodeObject;

@ObjectMapping("cf_t.TreePanel")
public class ExtTreePanel extends ExtComponent {

	@ObjectField(index = 1, defaultValue = "true")
	private boolean animate;

	@ObjectField(index = 2)
	private boolean autoScroll;

	@ObjectField(index = 3)
	private boolean border;

	@ObjectField(index = 4, defaultValue = "0")
	private String width;

	@ObjectField(index = 5, defaultValue = "0")
	private int height;

	@ObjectField(index = 6, defaultValue = "false")
	private boolean autoHeight;

	@ObjectField(index = 7, token = true)
	private String root = "root";

	@ObjectField(index = 8, token = true)
	private String loader = "loader";

	@ObjectField(index = 9, defaultValue = "true")
	private boolean rootVisible;

	@ObjectField(index = 10)
	private String renderTo;

	@ObjectField(index = 11)
	private String el;

	@ObjectField(index = 12)
	private boolean useArrows;

	@ObjectField(index = 13)
	private boolean containerScroll;

	@ObjectField(index = 14, defaultValue = "false")
	private boolean enableDD;

	@ObjectField(index = 15, mapping = "el", token = true)
	private String tokenEl;

	@ObjectField(index = 16, token = true)
	private String selModel;

	@ObjectField(index = 17, token = true)
	private String tbar;
	private String url;
	private String urlParams;
	private String rootText;
	private String data;
	private String onClick;
	private String onBeforeLoad;
	private String onCheckChange;
	private String contextMenu;
	private int level = 1;

	@ObjectField(index = 24, mapping = "monitorResize")
	protected boolean monitorResize = true;
	protected final BaseCodeObject<ExtTreePanel> codeObject = new BaseCodeObject<ExtTreePanel>() {
		@CodeMethod(index = 1)
		void treePanel() {
			ExtAsyncTreeNode localExtAsyncTreeNode = new ExtAsyncTreeNode();
			localExtAsyncTreeNode.setText(getRootText());
			localExtAsyncTreeNode.setChildren(getData());
			$JS.var(root, JS.instance(localExtAsyncTreeNode));
			ExtTreeLoader localExtTreeLoader = new ExtTreeLoader();
			localExtTreeLoader.setDataUrl(getUrl());
			localExtTreeLoader.setBaseParams(getUrlParams());
			$JS.var(loader, JS.instance(localExtTreeLoader));
			$JS.var(getId(), JS.instance(getContext()));
			if (getOnClick() != null) {
				$JS.method(getId() + ".on", new Expression[] { BASE.json("click"), BASE.token(getOnClick()) });
			}
				
			if (getOnBeforeLoad() != null) {
				$JS.method("loader.on", new Expression[] { BASE.json("beforeload"), BASE.token(getOnBeforeLoad()) });
			} else {
				$JS.method("loader.on", new Expression[] { BASE.json("beforeload"), BASE.token(getId() + "_beforeload_function") });
				$JS.function(getId() + "_beforeload_function", "treeLoader.baseParams.parentNodeId = node.id;", new String[] { "treeLoader", "node" });
			}
			if (getOnCheckChange() != null) {
				$JS.method(getId() + ".on", new Expression[] { BASE.json("checkChange"), BASE.token(getOnCheckChange()) });
			}
				
			$JS.function(getId() + "_render_function", "treePanel.body.setStyle('overflow', 'auto');", new String[] { "treePanel" });
			$JS.method(getId() + ".on", new Expression[] { BASE.json("render"), BASE.token(getId() + "_render_function") });
			$JS.method(getId() + ".render");
			$BASE.token("Wonders.utils.expandNode2Level(root," + getLevel() + ");");
			if (getContextMenu() != null) {
				$JS.method(getId() + ".on",
						new Expression[] {BASE.json("contextmenu"),
								BASE.token("function(node,e){e.preventDefault();node.select();Ext.menu.MenuMgr.get('"
										+ getContextMenu()
										+ "').showAt(e.getXY());}") });
			}	
		}
	};

	public String getJS() {
		this.codeObject.setContext(this);
		return CodeGenerator.generate(new CodeObject[] { this.codeObject });
	}

	public ExtTreePanel(String paramString) {
		super(paramString);
	}

	public boolean isRootVisible() {
		return this.rootVisible;
	}

	public String getRenderTo() {
		return this.renderTo;
	}

	public void setRootVisible(boolean paramBoolean) {
		this.rootVisible = paramBoolean;
	}

	public void setRenderTo(String paramString) {
		this.renderTo = paramString;
	}

	public String getLoader() {
		return this.loader;
	}

	public String getUrl() {
		return this.url;
	}

	public void setLoader(String paramString) {
		this.loader = paramString;
	}

	public void setUrl(String paramString) {
		this.url = paramString;
	}

	public boolean isAnimate() {
		return this.animate;
	}

	public boolean isAutoScroll() {
		return this.autoScroll;
	}

	public boolean isBorder() {
		return this.border;
	}

	public String getWidth() {
		return this.width;
	}

	public int getHeight() {
		return this.height;
	}

	public String getRoot() {
		return this.root;
	}

	public void setAnimate(boolean paramBoolean) {
		this.animate = paramBoolean;
	}

	public void setAutoScroll(boolean paramBoolean) {
		this.autoScroll = paramBoolean;
	}

	public void setBorder(boolean paramBoolean) {
		this.border = paramBoolean;
	}

	public void setWidth(String paramInt) {
		this.width = paramInt;
	}

	public void setHeight(int paramInt) {
		this.height = paramInt;
	}

	public void setRoot(String paramString) {
		this.root = paramString;
	}

	public String getRootText() {
		return this.rootText;
	}

	public void setRootText(String paramString) {
		this.rootText = paramString;
	}

	public String getData() {
		return this.data;
	}

	public void setData(String paramString) {
		this.data = paramString;
	}

	public String getEl() {
		return this.el;
	}

	public void setEl(String paramString) {
		this.el = paramString;
	}

	public boolean isUseArrows() {
		return this.useArrows;
	}

	public void setUseArrows(boolean paramBoolean) {
		this.useArrows = paramBoolean;
	}

	public boolean isContainerScroll() {
		return this.containerScroll;
	}

	public void setContainerScroll(boolean paramBoolean) {
		this.containerScroll = paramBoolean;
	}

	public String getUrlParams() {
		return this.urlParams;
	}

	public void setUrlParams(String paramString) {
		this.urlParams = paramString;
	}

	public boolean isEnableDD() {
		return this.enableDD;
	}

	public void setEnableDD(boolean paramBoolean) {
		this.enableDD = paramBoolean;
	}

	public String getOnClick() {
		return this.onClick;
	}

	public void setOnClick(String paramString) {
		this.onClick = paramString;
	}

	public String getOnBeforeLoad() {
		return this.onBeforeLoad;
	}

	public void setOnBeforeLoad(String paramString) {
		this.onBeforeLoad = paramString;
	}

	public String getOnCheckChange() {
		return this.onCheckChange;
	}

	public void setOnCheckChange(String paramString) {
		this.onCheckChange = paramString;
	}

	public String getTokenEl() {
		return this.tokenEl;
	}

	public void setTokenEl(String paramString) {
		this.tokenEl = paramString;
	}

	public String getSelModel() {
		return this.selModel;
	}

	public void setSelModel(String paramString) {
		this.selModel = paramString;
	}

	public boolean isAutoHeight() {
		return this.autoHeight;
	}

	public void setAutoHeight(boolean paramBoolean) {
		this.autoHeight = paramBoolean;
	}

	public String getTbar() {
		return this.tbar;
	}

	public void setTbar(String paramString) {
		this.tbar = paramString;
	}

	public boolean isMonitorResize() {
		return this.monitorResize;
	}

	public void setMonitorResize(boolean paramBoolean) {
		this.monitorResize = paramBoolean;
	}

	public String getContextMenu() {
		return this.contextMenu;
	}

	public void setContextMenu(String paramString) {
		this.contextMenu = paramString;
	}

	public int getLevel() {
		return this.level;
	}

	public void setLevel(int paramInt) {
		this.level = paramInt;
	}

	@ObjectMapping("cf_t.AsyncTreeNode")
	class ExtAsyncTreeNode {

		@ObjectField(index = 1)
		private String text;

		@ObjectField(index = 2)
		private boolean draggable = false;

		@ObjectField(index = 3)
		private String id = "0";

		@ObjectField(index = 4, token = true)
		private String children;

		public String getId() {
			return this.id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public boolean isDraggable() {
			return this.draggable;
		}

		public void setDraggable(boolean draggable) {
			this.draggable = draggable;
		}

		public String getText() {
			return this.text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public String getChildren() {
			return this.children;
		}

		public void setChildren(String children) {
			this.children = children;
		}
	}

	@ObjectMapping("cf_t.TreeLoader")
	class ExtTreeLoader {

		@ObjectField(index = 1)
		private String dataUrl;

		@ObjectField(index = 2, token = true)
		private String baseParams;

		public String getDataUrl() {
			return this.dataUrl;
		}

		public void setDataUrl(String dataUrl) {
			this.dataUrl = dataUrl;
		}

		public String getBaseParams() {
			return this.baseParams;
		}

		public void setBaseParams(String baseParams) {
			this.baseParams = baseParams;
		}
	}
}